var subsetsWithDup = function (nums) {
  let len = nums.length;
  nums.sort((a, b) => a - b);
  let res = [];
  let hash = {};
  function backtrack(path, start) {
    res.push(path);
    for (let i = start; i < len; i++) {
      // 解集不能包含重复的子集
      if (hash[i] || (i > 0 && !hash[i - 1] && nums[i - 1] === nums[i]))
        continue;
      hash[i] = true;
      path.push(nums[i]);
      backtrack(path.slice(), i + 1);
      hash[i] = false;
      path.pop();
    }
  }
  backtrack([], 0);
  return res;
};
